var common = require("../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        topImgWidth: 0,
        topImgHeight: 0,
        canvasHeight: 200
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        var that = this;
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        wx.setNavigationBarTitle({
            title: "二维码生成"
        });
        wx.showLoading({
            title: "二维码生成中"
        });
        // that.getImg()
                that.getBase();
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长"
                });
                that.canvas();
            }
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    canvas: function() {
        var rpx;
        var that = this;
        var leaderName = that.data.leaderName;
        wx.getSystemInfo({
            success: function(res) {
                rpx = res.windowWidth / 375;
            }
        });
        var topImg = that.data.domain + "img/tzsq.jpg";
        var topImgWidth, topImgHeight;
        var leaderName = that.data.leaderName;
        common.request({
            endpoint: "share/leader_leader",
            method: "post",
            data: {
                page: "pages/group_info/group_info"
            },
            success: res => {
                var ewmimg = res.extra.Qc;
                var poster = res.extra.leader_img;
                poster = poster.indexOf("https") < 0 ? poster.replace("http:", "https:") : poster;
                console.log(poster);
                wx.getImageInfo({
                    src: ewmimg,
                    success: function(res) {
                        console.log(11111);
                        wx.hideLoading();
                        var ph = 690 * 324 / 750 / 2;
                        console.log(ph);
                        const ctx = wx.createCanvasContext("myCanvas");
                        //底部白色矩形
                                                ctx.setFillStyle("#ffffff");
                        ctx.fillRect(0, ph * rpx, 345 * rpx, 145 * rpx);
                        ctx.setStrokeStyle("#dedede");
                        ctx.moveTo(0, ph);
                        ctx.lineTo(345 * rpx, ph);
                        ctx.stroke();
                        ctx.font = "18px bold";
                        ctx.fillText("长按识别小程序码", 25 * rpx, (ph + 35) * rpx);
                        ctx.setFillStyle("#ffffff");
                        ctx.setStrokeStyle("#ff7b19");
                        ctx.strokeRect(25 * rpx, (ph + 56) * rpx, 132 * rpx, 34 * rpx);
                        ctx.fillRect(29 * rpx, (ph + 57) * rpx, 127 * rpx, 30 * rpx);
                        //导入矩形框子
                                                ctx.font = "14px bold";
                        ctx.setFillStyle("#ff7b19");
                        ctx.fillText("立刻成为" + leaderName, 50 * rpx, (ph + 78) * rpx);
                        ctx.drawImage(res.path, 213 * rpx, (ph + 15) * rpx, 110 * rpx, 110 * rpx);
                        wx.getImageInfo({
                            src: poster,
                            success: function(res) {
                                ctx.drawImage(res.path, 0, 0, 345 * rpx, ph * rpx);
                                ctx.draw();
                                let canvasHeight = (ph + 145) * rpx * 2;
                                that.setData({
                                    canvasHeight: canvasHeight
                                });
                            }
                        });
                    },
                    fail: function() {}
                });
            }
        });
        //   }
        // })
        },
    saveimgdo: function() {
        var that = this;
        wx.getSetting({
            success(res) {
                if (!res.authSetting["scope.writePhotosAlbum"]) {
                    wx.authorize({
                        scope: "scope.writePhotosAlbum",
                        success() {
                            that.dosave();
                        },
                        fail() {
                            wx.openSetting();
                            wx.showModal({
                                title: "提示",
                                content: "未授权相册，请删小程序后再试！"
                            });
                        }
                    });
                } else {
                    that.dosave();
                }
            }
        });
    },
    dosave: function() {
        var that = this;
        var pwidth = that.data.pwidth;
        var pheight = that.data.heigts;
        var pixelRatio = that.data.pixelRatio;
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: pwidth,
            height: pheight,
            destWidth: pwidth * pixelRatio,
            destHeight: pheight * pixelRatio,
            canvasId: "myCanvas",
            success: function(res) {
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function(res) {
                        wx.showModal({
                            title: "提醒",
                            content: "请转发您专属的分享图至好友",
                            showCancel: false,
                            success: function(res) {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }
                        });
                    },
                    fail: function(res) {}
                });
            }
        });
    }
});